package com.qf.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

/**
 * 邮件发送工具类
 */
@Component
public class JavaMailUtil {

    @Autowired
    private JavaMailSender javaMailSender;

    //发件邮箱用户名
    @Value("${spring.mail.username}")
    private String username;

    //接收邮箱
    @Value("${spring.mail.to}")
    private String tos;


    @Async
    public void sendMailForQueue(String queueName,Integer messageCount) throws MessagingException {
        // 创建邮件
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();

        // 封装，发件人，收件人，主题，内容
        MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage,true);
        messageHelper.setFrom(username);
        messageHelper.setTo(tos.split(","));
        messageHelper.setSubject("快来啊,MQ队列要炸啦, 公司炸没了, 没人发工资啦!!!");
        messageHelper.setText("text/html;charset=utf-8","<h1 style='color:red;'>RabbitMQ中"+queueName+"的消息堆积了，消息已经达到了"+messageCount+"条，抓紧时间跑路吧，删库跑路！！！</h1>");

        // 发送
        javaMailSender.send(mimeMessage);
    }
}
